﻿/*  ----------------------------------------------------------------------------
 *  Warren Moxley
 *  ----------------------------------------------------------------------------
 *  GoogleHelper
 *  ----------------------------------------------------------------------------
 *  File:       TaskListHelper.cs
 *  Author:     IT1SERVICES\wmoxley
 *  ----------------------------------------------------------------------------
	Licensed to the Apache Software Foundation (ASF) under one
       	or more contributor license agreements.  See the NOTICE file
       	distributed with this work for additional information
       	regarding copyright ownership.  The ASF licenses this file
       	to you under the Apache License, Version 2.0 (the
       	"License"); you may not use this file except in compliance
       	with the License.  You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

       	Unless required by applicable law or agreed to in writing,
       	software distributed under the License is distributed on an
       	"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       	KIND, either express or implied.  See the License for the
       	specific language governing permissions and limitations
       	under the License. 
 *  ----------------------------------------------------------------------------
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Google.Apis;
using Google.Apis.Discovery;
using Google.Apis.Requests;
using GoogleRequests = Google.Apis.Requests;
using Google.Apis.Authentication;
using Google.Apis.Wrappers.Tasks;
using Data = Google.Apis.Wrappers.Tasks.Data;

using DotNetOpenAuth;
using DotNetOpenAuth.OAuth2;
using System.Web;

namespace GoogleHelper
{
    public class TaskListHelper
    {
        #region Private Variables

        string userName;
        string password;

        #endregion

        #region Constructors

        public TaskListHelper(string userName, string password)
        {
            this.userName = userName;
            this.password = password;
        }

        #endregion

        #region List Methods

        public Data.TaskLists GetTaskLists(string pageToken)
        {
            string loginType = "cl";

            IAuthenticator auth = new ClientAuth(new PrefetchedUserNamePasswordSupplier(userName, password), loginType, Properties.Settings.Default.applicationName);
            
            TasksService svc = new TasksService(auth, Properties.Settings.Default.apiKey, Properties.Settings.Default.applicationName);

            Tasklists tl = new Tasklists(svc);

            return tl.ListAsObject(100, pageToken);

        }
       
        #endregion

        #region Get Methods

        public Data.TaskList GetTaskList(string taskListId)
        {
            string loginType = "cl";

            IAuthenticator auth = new ClientAuth(new PrefetchedUserNamePasswordSupplier(userName, password), loginType, Properties.Settings.Default.applicationName);

            TasksService svc = new TasksService(auth, Properties.Settings.Default.apiKey, Properties.Settings.Default.applicationName);

            Tasklists tl = new Tasklists(svc);

            return tl.GetAsObject(taskListId);

        }

        #endregion

        #region Update Methods

        public Data.TaskList UpdateTaskList(Data.TaskList taskList, string taskListId)
        {
            string loginType = "cl";

            IAuthenticator auth = new ClientAuth(new PrefetchedUserNamePasswordSupplier(userName, password), loginType, Properties.Settings.Default.applicationName);

            TasksService svc = new TasksService(auth, Properties.Settings.Default.apiKey, Properties.Settings.Default.applicationName);

            Tasklists tl = new Tasklists(svc);
            return tl.UpdateAsObject(taskList, taskListId);
            
        }

        public string ClearCompletedTaskList(string taskListId)
        {
            string loginType = "cl";

            IAuthenticator auth = new ClientAuth(new PrefetchedUserNamePasswordSupplier(userName, password), loginType, Properties.Settings.Default.applicationName);

            TasksService svc = new TasksService(auth, Properties.Settings.Default.apiKey, Properties.Settings.Default.applicationName);

            Tasks tl = new Tasks(svc);
            return new StreamReader(tl.ClearAsObject("", taskListId)).ReadToEnd();

        }


        #endregion

        #region Insert Methods

        public Data.TaskList InsertTaskList(Data.TaskList taskList)
        {
            string loginType = "cl";

            IAuthenticator auth = new ClientAuth(new PrefetchedUserNamePasswordSupplier(userName, password), loginType, Properties.Settings.Default.applicationName);

            TasksService svc = new TasksService(auth, Properties.Settings.Default.apiKey, Properties.Settings.Default.applicationName);

            Tasklists tl = new Tasklists(svc);

            return tl.InsertAsObject(taskList);

        }

        #endregion

        #region Delete Methods

        public string DeleteTaskList(string taskListId)
        {
            string loginType = "cl";

            IAuthenticator auth = new ClientAuth(new PrefetchedUserNamePasswordSupplier(userName, password), loginType, Properties.Settings.Default.applicationName);

            TasksService svc = new TasksService(auth, Properties.Settings.Default.apiKey, Properties.Settings.Default.applicationName);

            Tasklists tl = new Tasklists(svc);

            return new StreamReader(tl.DeleteAsObject(taskListId)).ReadToEnd();

        }

        #endregion

    }

}
